package com.liuhuxiang.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.liuhuxiang.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * 新建订单服务
 *
 * @author liuhuxiang
 * @version: OrderController.java, 2023年02月27日 下午12:13 liuhuxiang  Exp $
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/add")
    public String add() {
        System.out.println("下单成功");
        return "Hello World";
    }

    @RequestMapping("/flow")
    // 先注释掉SentinelResource，测试下能否用上统一的异常处理MyBlockExceptionHandler
    //@SentinelResource(value = "flow",blockHandler = "flowBlockHandler")
    public String flow() {
        System.out.println("下单成功");
        return "访问成功";
    }

    /***
     * 测试线程流控方式
     * @return
     */
    @RequestMapping("/flowThread")

    // 先注释掉SentinelResource，测试下能否用上统一的异常处理MyBlockExceptionHandler
    //@SentinelResource(value = "flowThread",blockHandler = "flowBlockHandler")
    public String flowThread() throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        return "访问成功";
    }

    /***
     * 流控规则
     * @param e
     * @return
     */
    public String flowBlockHandler(BlockException e) {
        return "流控";
    }

    /*********************************************关联流控*********************************************/
    @RequestMapping("/addOrder")
    public String addOrder() {
        System.out.println("下单成功!");
        return "生成订单";
    }

    // 关联流控  访问/add 触发/get
    @RequestMapping("/getOrder")
    public String getOrder() throws InterruptedException {
        return "查询订单";
    }



    /************************链路流控***********************/
    @Autowired
    IOrderService orderService;

    // 链路流控   访问/add 触发/get
    @RequestMapping("/test1")
    public String test1() {
        return orderService.getUser();
    }

    // 链路流控  访问/add 触发/get
    @RequestMapping("/test2")
    public String test2() throws InterruptedException {
        return orderService.getUser();
    }


    /************************降级规则--异常比例***********************/
    @RequestMapping("/err")
    public String err()  {
        int a=1/0;
        return "hello";
    }

    /************************热点参数流控***********************/
    /**
     * 热点规则，必须使用@SentinelResource
     *
     * 使用@SentinelResource，就不能再使用全局异常处理了
     * @param id
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/get/{id}")
    @SentinelResource(value = "getById",blockHandler = "HotBlockHandler")
    public String getById(@PathVariable("id") Integer id) throws InterruptedException {

        System.out.println("正常访问");
        return "正常访问";
    }

    public String HotBlockHandler(@PathVariable("id") Integer id,BlockException e) throws InterruptedException {

        return "热点异常处理";
    }
}